package com.voice.server.service.impl;

import com.voice.server.converter.FunctionConverter;
import com.voice.server.entity.Function;
import com.voice.server.enums.ResultEnum;
import com.voice.server.exception.SystemException;
import com.voice.server.output.TreeNode;
import com.voice.server.repository.FunctionRepository;
import com.voice.server.repository.RoleFunctionRepository;
import com.voice.server.service.FunctionService;
import com.wangjiacai.hqlQuery.service.impl.BaseServiceImpl;
import com.wangjiacai.utils.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2019-01-27 18:09:41
 */
@Service
public class FunctionServiceImpl extends BaseServiceImpl implements FunctionService {

    @Autowired
    private FunctionRepository functionRepository;

    @Autowired
    private RoleFunctionRepository roleFunctionRepository;

    @Override
    public List<TreeNode> getTreeNode() {
        Sort sort=new Sort(Sort.Direction.ASC,"bh");
        List<Function> functions = functionRepository.findAll(sort);
        List<TreeNode> list=new ArrayList<>();
        for (Function func : functions) {
            TreeNode node = new TreeNode();
            node.setText(func.getName());
            node.setId(func.getId());
            node.setBh(func.getBh());
            node.setIcon(func.getIcon());
            if(func.getBh().length()==6){
                list.add(node);
            }else {
                TreeNode current=list.get(list.size()-1);
                if (current.getNodes() == null)
                    current.setNodes(new ArrayList<>());
                current.getNodes().add(node);
            }
        }
        return list;
    }

    @Override
    public List<Map> getFunctionTreeMap() {
        Sort sort=new Sort(Sort.Direction.ASC,"bh");
        List<Function> list=functionRepository.findAll(sort);
        return FunctionConverter.converterToMapList(list);
    }

    @Override
    public Function findById(String functionId) {
        return functionRepository.getOne(functionId);
    }

    @Override
    public void deleteById(String functionId) {
      roleFunctionRepository.deleteByFunctionId(functionId);
      functionRepository.deleteById(functionId);
    }

    @Override
    public Function addFunction(Function function) {
        return functionRepository.save(function);
    }

    @Override
    public Function updateFunction(Function function) {
        Function target=functionRepository.getOne(function.getId());
        if(target==null) throw new SystemException(ResultEnum.ILLEGAL_USERS);
        BeanUtil.copyPropertiesIgnoreEmpty(function,target);
        return functionRepository.save(target);
    }

    @Override
    public List<String> findBhListByRoleId(String roleId) {
        List<String> functionIds=roleFunctionRepository.findFunctionIdByRoleId(roleId);
        if(functionIds==null||functionIds.size()==0) return null;
        return functionRepository.findBhByIdInAndUrlIsNotNull(functionIds);
    }
}
